import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:ty_smart_home/ty_smart_home.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();

    checkPermission();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await TySmartHome.platformVersion;

      _loginApp();

    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),

              TextButton(
                child: Text("开始配网"),
                onPressed: () async {
                  log("开始配网");
                  final homeId = await TySmartHome.getHomeID();
                  // 开始配网
                  TySmartHome.commitEZModeAction(homeId, "515", "88888888");    //  mi, 12345678  515  88888888
                },
              ),

              TextButton(
                child: Text("获取设备列表"),
                onPressed: () async {
                  final list = await TySmartHome.getDeviceList();
                  log("设备1: $list");
                  list.forEach((element) {
                     log("设备2: $element");
                  });
                },
              ),

              TextButton(
                child: Text("分享设备"),
                onPressed: () async {
                  final list = await TySmartHome.inviteShare("deviceId", "86", "uid");

                },
              ),

            ],
          ),
        ),
      ),
    );
  }

  // 检测定位权限是否开启
  Future<void> checkPermission() async {
    // undetermined未定, granted被授予, denied被拒绝, restricted受限制的, permanentlyDenied永久拒绝
    if (Platform.isAndroid || Platform.isIOS) {
      final isLocationStatus = await Permission.location.request().isGranted;
      final isBLEStatus = await Permission.bluetooth.request().isGranted;

      if (!isLocationStatus || !isBLEStatus) {
        // logs('需要获取音视频权限才能进入');
        return;
      }
    }
  }

  _loginApp() {
    // 登录App
    TySmartHome.loginApp("86", "13396075193", "15071196344").then((value) {
      if (value == "1") {  // 登录成功
        print("登录成功");
        TySmartHome.getDeviceList();
      }
    });

    return;

    TySmartHome.loginOrRegisterWithUid("86", "123456", "123456").then((value) {
      if(value["result"] == "1"){  // 登录成功
        final tyUID = value["uid"];
      }
      print("涂鸦登录结果: $value");
    });
  }
}
